package com.wavecoler.professor.bots;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.SortedMap;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import com.google.appengine.api.datastore.Key;
import com.google.wave.api.AbstractRobot;
import com.google.wave.api.Blip;
import com.google.wave.api.Element;
import com.google.wave.api.ElementType;
import com.google.wave.api.FormElement;
import com.google.wave.api.event.FormButtonClickedEvent;
import com.google.wave.api.event.WaveletBlipCreatedEvent;
import com.google.wave.api.event.WaveletSelfAddedEvent;
import com.google.appengine.api.datastore.KeyFactory;
import com.wavecoler.dao.PMF;
import com.wavecoler.professor.objects.Team;
import com.wavecoler.professor.objects.User;

@SuppressWarnings("serial")
public class BotTeamReader extends AbstractRobot {

	private static Logger LOG = Logger.getLogger(BotTeamReader.class.getName());
	private PersistenceManager pm;

	@Override
	protected String getRobotName() {
		return "Prueba Users";
	}

	@Override
	protected String getRobotProfilePageUrl() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onWaveletSelfAdded(WaveletSelfAddedEvent event) {

		event.getWavelet().setTitle("Prueba Team Reader");

		Blip blip = event.getBlip();

		blip.append("\n");
		blip.append(new Element(ElementType.LABEL, createProperties("lblFName",
				"Team:")));
		blip.append("\n");
		blip.append(new Element(ElementType.INPUT, createProperties(
				"txtAccount", "type Team name here")));
		blip.append("\n");
		blip.append(new Element(ElementType.BUTTON, createProperties(
				"btnStart", "Start Session")));

		/*
		 * User d = new User(); pm.makePersistent(d); pm.close();
		 */
	}

	@Override
	public void onWaveletBlipCreated(WaveletBlipCreatedEvent event) {
		// TODO Auto-generated method stub

	}

	private HashMap<String, String> createProperties(String name, String value) {
		HashMap<String, String> properties = new HashMap<String, String>();
		properties.put("name", name);
		properties.put("value", value);
		return properties;
	}

	@Override
	public void onFormButtonClicked(FormButtonClickedEvent e) {
		LOG.info("onFormButtonClicked");
		Blip blip = e.getBlip();
		SortedMap<Integer, Element> elements = e.getBlip().getElements();
		String button = e.getButtonName();
		String TeamName = elements.get(24).getProperty("value");
		pm = PMF.get().getPersistenceManager();
		Query query = pm.newQuery("select from " + Team.class.getName()
				+ " where TeamName == :p1");
		List<Team> Teams = (List<Team>) query.execute(TeamName);
		Team Team = Teams.get(0);
		/*Set<Key> participants = (HashSet<Key>) Team.getParticipants();
		Iterator itr = participants.iterator();
		while (itr.hasNext()) {
			Key k = (Key) itr.next();
			 User u = pm.getObjectById(User.class, k);
			 String account = u.getUserName().toString().substring(6);
			 account = account.replace("\")" , "");
			blip.reply().append(account);
			e.getWavelet().getParticipants().add(account);
		}*/
		// blip.reply().append(users.get(0).getUserName());

		Blip reply = blip.reply();
		reply.append(e.getButtonName());

		SortedMap<Integer, Element> elements2 = e.getBlip().getElements();
		HashMap<String, String> responses = new HashMap<String, String>();

		for (Integer key : elements2.keySet()) {
			Element elem = elements2.get(key);
			if (elem.isFormElement()) {
				if (elem.getType() != ElementType.LABEL
						&& elem.getType() != ElementType.BUTTON) {
					responses.put(elem.getProperty("name"),
							elem.getProperty("value"));
				}
			}
		}

		// Check what was written
		for (String response : responses.keySet()) {
			reply.append("\nIn the field: '" + response + "' You typed: '"
					+ responses.get(response) + "'");
		}

	}

	private String getValue(Element fe) {
		Element elem = fe;
		String value = "";
		if (elem.isFormElement()) {
			if (elem.getType() != ElementType.LABEL
					&& elem.getType() != ElementType.BUTTON) {
				value = elem.getProperty("value");
			}
		}
		return value;
	}

}
